//引入api
import { getAdminTopic, postAddtopic, postPutimg } from "@/request/api.js";

//引入element
import { Message } from 'element-ui';


//引入路由
import router from '@/router'

export default {
    namespaced: true,
    state: {
        pageSize: 10,
        count: 100,
        tableData: [{}],
        currentPage: 1,
        subsign: {
            title: '',
            subtitle: '',
            price_info: '',
            is_show: false,
            sort_order: '',
            scene_pic_url: '',
            content:''
        }
    },
    mutations: {
        //初始化参数
        goodscreatedsubsign(state, payload) {
            state.subsign = { ...payload }
        },
        //修改本地vuex
        goodsGetvalue(state, payload) {
            state.subsign = payload
        },
        topicattributeList(state, payload) {
            state.tableData = payload.data
            state.count = payload.count
            state.currentPage = payload.currentPage
            state.pageSize = payload.pageSize
        },
        //获取编辑数据
        goodseditsubsign(state, payload) {
            console.log('编辑前的参数', state.subsign)

            state.subsign = { ...payload }
            //对值做相应调整
            if (payload.is_show) {
                state.subsign.is_show = true
            } else {
                state.subsign.is_show = false
            }
        }
    },
    actions: {
        //获取商品属性
        topicgetAdmin({ commit, state }, payload = payload || {}) {
            getAdminTopic({
                page: payload.currnet || state.currentPage,
                size: payload.size || state.pageSize
            }).then(res => {
                console.log("获取topic", res.data)
                commit('topicattributeList', res.data)
                // console.log(state.tableData)
            })
        },
        //上传图片
        categoryPutimg01({ commit, state }, payload) {
            console.log("查看传值", payload)
            postPutimg(
                payload
            ).then(res => {
                console.log(res.data)
                state.subsign.scene_pic_url = res.data.fileUrl
            })
        },
        //发起请求
        goodsAddgoods({ commit, state }) {
            let is_show = 0;
            if (state.subsign.is_show) {
                is_show = 1
            } else {
                is_show = 0
            }
            postAddtopic({
                ...state.subsign,
                is_show
            }).then(res => {

                if (res) {
                    if(state.subsign.id){
                        Message({
                            message:'修改成功',
                            type:'success'
                        })
                    }else{
                        Message({
                            message:'添加成功',
                            type:'success'
                        })
                    }
                    router.push('/topic')
                } else {
                    console.log('请求失败')
                }
            })
        },
    }
}